package com.wpam.scrabble.mobile.activities;

import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.wpam.scrabble.mobile.R;

public class GameSettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener, OnClickListener {

	public final static String SERVER_TYPE = "serverType";
	public final static String TURN_TIME = "turnTime";
	public final static String WORD_CHECK = "wordCheck";
	
	ListPreference serverTypePref_;
	EditTextPreference turnTimePref_;
	CheckBoxPreference checkWordsPref_;
	
	Button startButton;
	
    @SuppressWarnings("deprecation")
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.prefs);
        setContentView(R.layout.game_settings);

        getListView().setCacheColorHint(0);
        
        serverTypePref_ = (ListPreference) findPreference(SERVER_TYPE);
        turnTimePref_ = (EditTextPreference) findPreference(TURN_TIME);
        checkWordsPref_ = (CheckBoxPreference) findPreference(WORD_CHECK);
        
        turnTimePref_.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
        
        serverTypePref_.setSummary(PreferenceManager.getDefaultSharedPreferences(this).getString(SERVER_TYPE, ""));
        turnTimePref_.setSummary(PreferenceManager.getDefaultSharedPreferences(this).getString(TURN_TIME, "") + " " + this.getApplication().getString(R.string.minutes));
        checkWordsPref_.setSummary(getResources().getString(R.string.requires_internet_connection));
        
        startButton = (Button) findViewById(R.id.startWaitingButton);
        startButton.setOnClickListener(this);
    }

	@SuppressWarnings("deprecation")
	@Override
	protected void onPause() {
		super.onPause();
		getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
	}

	@SuppressWarnings("deprecation")
	@Override
	protected void onResume() {
		super.onResume();
		getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
	}

	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		
		if (key.equals(SERVER_TYPE)) {
			serverTypePref_.setSummary(PreferenceManager.getDefaultSharedPreferences(this).getString(SERVER_TYPE, ""));
			onContentChanged();
			return;
		}
		if (key.equals(TURN_TIME)) {
			turnTimePref_.setSummary(sharedPreferences.getString(TURN_TIME, "") + " " + this.getApplication().getString(R.string.minutes));
			onContentChanged();
			return;
		}
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.startWaitingButton:
			if (PreferenceManager.getDefaultSharedPreferences(this).getString(SERVER_TYPE,  "").equals(getResources().getString(R.string.web_server))) {
				Builder builder = new Builder(v.getContext());
				builder.setTitle(getResources().getString(R.string.create_game));
				builder.setMessage(getResources().getString(R.string.not_implemented));
				builder.setPositiveButton(getResources().getString(R.string.yes), new DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
				builder.create().show();
				break;
			}
			Intent waitingIntent = new Intent(this, PlayersWaitingActivity.class);
			startActivity(waitingIntent);
			break;
		}
		
	}
    
    
}
